package com.kanshu.ksgb.fastread.doudou.module.download.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.business.dialog.CommonDialog;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.view.CustomDialog;
import com.kanshu.ksgb.fastread.doudou.module.download.bean.SimpleBookInfo;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.CacheManager;
import com.kanshu.ksgb.fastread.doudou.module.download.utils.DownLoadHelper;

import java.util.List;

/**
 * Created by xubo on 2018/11/27
 */

public class DownLoadCenterAdapter extends BaseQuickAdapter<SimpleBookInfo> {

    public DownLoadCenterAdapter(Context context, List<SimpleBookInfo> data) {
        super(context, data);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_download_center;
    }

    @Override
    protected void convert(BaseViewHolder holder, SimpleBookInfo item, int position) {
        holder.setText(R.id.book_title, item.book_title)
                .setText(R.id.book_author, "作者:" + item.author_name);
        DisplayUtils.gone(holder.getView(R.id.again_download), holder.getView(R.id.clear_download_book));
        switch (item.download_state){
            case SimpleBookInfo.STATE_HAS_DOWNLOAD_ALL:
                holder.setText(R.id.book_download_state, "已下载  共" + item.download_size);
                DisplayUtils.visible(holder.getView(R.id.clear_download_book));
                break;
            case SimpleBookInfo.STATE_IS_DOWNLOAD:
                holder.setText(R.id.book_download_state, "下载中");
                break;
            case SimpleBookInfo.STATE_WAITING_DOWNLOAD:
                holder.setText(R.id.book_download_state, "等待下载");
                break;
            case SimpleBookInfo.STATE_PART_DOWNLOAD:
                holder.setText(R.id.book_download_state, "已下载" + item.download_count + "章节  共" + item.download_size);
                DisplayUtils.visible(holder.getView(R.id.clear_download_book));
                break;
            case SimpleBookInfo.STATE_HAS_DOWNLOAD_FAIL:
                holder.setText(R.id.book_download_state, "下载失败");
                DisplayUtils.visible(holder.getView(R.id.again_download));
                holder.getView(R.id.again_download).setOnClickListener(v -> {
                    DownLoadHelper.againDownload(v.getContext(), item.book_id);
                    notifyDataSetChanged();
                });
                DisplayUtils.visible(holder.getView(R.id.clear_download_book));
                break;
        }
        holder.getView(R.id.clear_download_book).setOnClickListener(v -> {
            CommonDialog.show((Activity) mContext, "确定删除此书所有下载内容？\n（收藏、阅读进度不受影响）", new CustomDialog.Callback() {
                @Override
                public void onSure(Dialog dialog) {
                    DownLoadHelper.clearDownLoadBook(item.book_id);
                    dialog.dismiss();
                }

                @Override
                public void onCancel(Dialog dialog) {
                    dialog.dismiss();
                }
            }).setSureText("删除");
        });
        GlideImageLoader.load(item.cover_url, holder.getView(R.id.book_cover));
    }
}
